from openpyxl import Workbook
from openpyxl.compat import range
from openpyxl.utils import get_column_letter

from openpyxl.styles import colors
from openpyxl.styles import Color

from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font
from scanf import scanf

font = Font(name='Calibri',
            size=11,
            bold=False,
            italic=False,
            vertAlign=None,
            underline='none',
            strike=False,
            color='FF000000')
fill = PatternFill(fill_type=None,
                   start_color='FFFFFFFF',
                   end_color='FF000000')
border = Border(left=Side(border_style=None,
                          color='FF000000'),
                right=Side(border_style=None,
                           color='FF000000'),
                top=Side(border_style=None,
                         color='FF000000'),
                bottom=Side(border_style=None,
                            color='FF000000'),
                diagonal=Side(border_style=None,
                              color='FF000000'),
                diagonal_direction=0,
                outline=Side(border_style=None,
                             color='FF000000'),
                vertical=Side(border_style=None,
                              color='FF000000'),
                horizontal=Side(border_style=None,
                                color='FF000000')
                )
alignment = Alignment(horizontal='center',
                      vertical='center',
                      text_rotation=0,
                      wrap_text=False,
                      shrink_to_fit=False,
                      indent=0)
number_format = 'General'
protection = Protection(locked=True,
                        hidden=False)


def main():
    wb = Workbook()

    dest_filename = 'empty_book.xlsx'

    ws1 = wb.active
    ws1.title = "eval"

    column = ['type', 'dataset', 'variants', 'lr', 'dslr', 'clip', 'slow start', 'hidden size',
              'explr', 'final(Fave)', 'max(Fave)', 'decay(Fave)', 'ave(decay(Fsplit))',
              'S final(Fave)', 'S max(Fave)', 'S decay(Fave)', 'S ave(decay(Fsplit))',
              'total time', 'epk', 'final F1-5']

    a1 = ws1['A1']
    ft = Font(bold=True)  # Font(color=colors.RED, bold=True)
    a1.font = ft

    # If you want to change the color of a Font, you need to reassign it::
    # a1.font = Font(color=colors.RED, italic=True) # the change only affects A1
    for j, col in enumerate(column, 1):
        # f"{get_column_letter(col)}"
        cell = ws1.cell(column=j, row=1, value=col)
        cell.font = ft
        cell.alignment = alignment
        cell.guess_types

    wb.save(filename=dest_filename)


def parse_log(root):
    """parse log in root/*.log

    :root: log root dir
    :returns: info about log

    """

    # TODO 没有必要完全匹配行, no need scanf("{'clip': %f,\r", "{'clip': 5.0,\r")
    collect = {'clip':          "'clip': %f",
               'dataset':       "'dataset': '%s'",
               'dslr':          "'discriminator_lr': %f",
               'slow_start':    "'discriminator_slow_start': %d",
               'lr':            "'lr': %f",
               'epk':           "'n_epochs': %d",
               'variants':      "'shift': '%s'",
               'hs':            "eLSTM input_size: %*d hidden_size: %d",
               'total_time':    "Total time: %s",
               'suffix':        "'suffix': '%s'"
               }
    info = {}
    with open(root, 'r') as f:
        for ln in f:
            for key, val in collect.items():
                parse = scanf(val, ln)
                if parse is not None:
                    info[key] = parse[0]
    return info


if __name__ == "__main__":
    info = parse_log('/home/zgp/data/results/canonical/summe_canonical_dpp_dslr1.00e-05_lr1.00e-04_clip5.0_slow15_hs2048_explr_2018/f2.log')
    __import__('ipdb').set_trace()
